﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;

public partial class Admin_AdminProductsIphoneAddNew : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        if(!IsPostBack){
            bindDataToDropDownIphoneDetails();
        }
    }
    protected void getInsertIphone(string ID, string name, float price, string img)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spAddnewIphoneProducts", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
        command.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;
        command.Parameters.Add("@TypeName", SqlDbType.VarChar).Value = "iPhone";
        command.Parameters.Add("@Price", SqlDbType.Float).Value = price;
        command.Parameters.Add("@Image", SqlDbType.VarChar).Value = img;
        command.ExecuteNonQuery();
        connect.Close();
    }
    protected void btnAddNewIphone_Click(object sender, EventArgs e)
    {
        string filename = "";
        bool check = false;

        string id1, name1, img1;
        float price1;
        id1 = txtIphoneID.Text;
        name1 = txtIphoneName.Text;
        price1 = float.Parse(txtIphonePrice.Text);


        if (fileUploadeIphone.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileUploadeIphone.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileUploadeIphone.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(fileUploadeIphone.FileName);
                fileUploadeIphone.SaveAs(Server.MapPath("~/IMG/iPhone/") + filename);
                

                lblStatus.Visible = true;
                //.ImageUrl = "~/IMG/" + filename;
                //Image1.Visible = true;
            }
            else
            {
                lblStatus.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {


            img1 = "~/IMG/iPhone/" + filename;
            getInsertIphone(id1, name1, price1, img1);
            imageIphone.ImageUrl = img1;
            bindDataToDropDownIphoneDetails();
            lblStatus.Text = "Insert Successfully";
      
        }
        else if (check == false)
        {
            lblStatus.Text = "please choose Image";
        }
    }

    protected void getInsertIphoneDetails(string ID, string img, string text)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spAddnewIphoneDetails", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
        command.Parameters.Add("@Image", SqlDbType.VarChar).Value = img;
        command.Parameters.Add("@Text", SqlDbType.VarChar).Value = text;
        command.ExecuteNonQuery();
        connect.Close();
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        string filename = "";
        bool check = false;

        string id1,text1, img1;
        id1 = txtDetailsID.Text;
        text1 = txtDetailsText.Text;


        if (fileUploadIphoneDetails.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileUploadIphoneDetails.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileUploadIphoneDetails.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(fileUploadIphoneDetails.FileName);
                fileUploadIphoneDetails.SaveAs(Server.MapPath("~/IMG/iPhone/") + filename);
               

                lblStatus1.Visible = true;
                //.ImageUrl = "~/IMG/" + filename;
                //Image1.Visible = true;
            }
            else
            {
                lblStatus1.Text = "File must be Images and less than 500kb";
                lblStatus1.Visible = true;
            }
        }

        if (check == true)
        {


            img1 = "~/IMG/iPhone/" + filename;
            getInsertIphoneDetails(id1, img1, text1);
            imageIphoneDetails.ImageUrl = img1;
            lblStatus1.Text = "Insert Successfully";

        }
        else if (check == false)
        {
            lblStatus1.Text = "please choose Image";
        }
    }
    protected void bindDataToDropDownIphoneDetails()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            //ds = new DataSet();
            command = new SqlCommand("spGetProductIphone", connect);
            command.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(command);
            da.Fill(ds, "ProductID");
            int a = ds.Tables["ProductID"].Rows.Count;
            dropDownIphoneDetails.Items.Clear();
            for (int i = 0; i < a; i++)
            {
                dropDownIphoneDetails.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
            }

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnSeletedDropDown_Click(object sender, EventArgs e)
    {
        txtDetailsID.Text = dropDownIphoneDetails.Text;
        btnAddNewIphoneDetails.Enabled = true;
    }
}